<template>
  <el-dialog title="编辑员工" :visible="showEditDialog" @close="closeFn">
    <el-form
      ref="form"
      :model="formData"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" type="email" autocomplete="off" />
      </el-form-item>
      <!-- 上传图片 -->
      <el-row type="flex" justify="center">
        <UploadImg ref="headImg" :span="5" style="margin-left: 15px " :limit-img="1" @addEvent="uploadImgFn" />
      </el-row>
      <el-row type="flex" justify="center" style="margin-top: 20px">
        <el-col :span="5" style="margin-left: 35px ">
          <el-button @click="closeFn">取消</el-button>
          <el-button type="primary" @click="confirmFn">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getUserDetail, editEmployee } from '@/api/employees'
import UploadImg from '@/components/ImageUpload/index copy.vue'
export default {
  components: { UploadImg },
  props: {
    showEditDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        // 表单数据
        username: '',
        email: '',
        roles: [
          {
            title: '',
            description: ''
          }
        ]
      },
      rules: {
        // 表单 验证
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在2-6个字符长度', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          {
            pattern: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/,
            message: '邮箱格式不正确',
            trigger: 'blur'
          }
        ]
      },
      imageUrl: ''
    }
  },
  methods: {
    closeFn() {
      // 关闭按钮
      this.$emit('update:showEditDialog', false)
      this.formData = {
        // 表单数据
        username: '',
        email: ''
      }
      this.$refs.headImg.fileList = [] // 关闭弹窗时清空上次显示的图片
      this.$refs.form.resetFields()
    },
    async getUserDetailFn(id) {
      this.formData = await getUserDetail(id)
      if (!this.$refs.headImg.fileList[0]) { // 回显图片
        this.$refs.headImg.fileList.push({ url: this.formData.image })
      }
    },
    async confirmFn() {
      // 确认按钮
      this.formData.image = this.imageUrl
      await editEmployee(this.formData)
      this.$message.success('编辑成功')
      this.$emit('updateEmployee')
      this.closeFn()
    },
    uploadImgFn(data) {
      if (data[0] && data[0].url) {
        this.imageUrl = data[0].url
      }
    }
  }
}
</script>

<style lang="scss">
/* .el-upload,.el-upload-list {
  position: relative;
  top: 85px;
  left: 50%;
  transform: translate(-50%,-50%);
  .el-upload-list__item {
    position: relative;
    top: -75px;
    left: -75px;
  }
} */

</style>
